Enable generic TypedDicts#13389
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
It is great to have |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The |
This comment has been minimized.
This comment has been minimized.
|
Hm, something came to my mind, I only support class syntax for generic TypedDicts, but it seems to me we can also allow the assignment-based syntax (it would be similar to how we define generic type aliases): TD = TypedDict("TD", {"key": int, "value": T}) # T must be unbound here.This should be not hard to add here. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Some potential merge conflicts already start to appear, it would be great if someone can take a look at this soon. |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Looks good, just one suggestion in the tests.
| x: str | ||
| y: str | ||
|
|
||
| reveal_type(TD.__iter__) # N: Revealed type is "def (typing._TypedDict) -> typing.Iterator[builtins.str]" |
There was a problem hiding this comment.
Isn't this just the keys? Maybe test .values() too.
This comment has been minimized.
This comment has been minimized.
1 similar comment
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Fixes #3863
This builds on top of some infra I added for recursive types (Ref #13297). Implementation is quite straightforward. The only non-trivial thing is that when extending/merging TypedDicts, the item types need to me mapped to supertype during semantic analysis. This means we can't call
is_subtype()etc., and can in theory get types likeUnion[int, int]. But OTOH this equally applies to type aliases, and doesn't seem to cause problems.Btw the diff in
semanal_typeddict.pyis not as big as it looks. There was a giantif possible: ...that I found a bit annoying and replaced it withif not possible: return(IIRC GitHub has an option to ignore whitespace).An important note on runtime support for this: IIUC this should work in Python 3.11, and should work soon with
typing_extensionson earlier versions. I didn't add any version checks here. If someone is interested in adding those, please do this.